package com.example.AOPTest.proxy.jdkTest;

import com.example.AOPTest.proxy.jdkTest.impl.RealSubject;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * description: ProxyFactory <br>
 * date: 2021/4/23 11:18 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class ProxyFactory implements InvocationHandler{

    private Object target;// 维护一个目标对象

    public ProxyFactory(Object target) {
        this.target = target;
    }

    // 为目标对象生成代理对象
    public Object getProxyInstance() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("计算开始时间");
        // 执行目标对象方法
        Object result = method.invoke(target, args);
        System.out.println("计算结束时间");
        return result;
    }
}
